#include "MainHeader.h"
//Get the Major Arrays
//Currently only one exists
/*
int GetArrays(){
	int i=0,ii=0;;
	u32 myoffset=0x833DFDC;
	
	/*
	Get FileNo, HeaderPointers + 1, RoomOffsets
	Get FileNo, DoorPointers + 1, DoorOffsets
	Get FileNo, SpecialEffectTablePnt + 1, SpecialEffects
	Get FileNo, MiniMapBanks + 1, ZMMiniMapOffsets()
	Get FileNo, ZMScrollListBase + 1, ZMScrolls()
	*//*
	memset(&RoomTilesets,0,0x500);
	fclose(GBA.ROM);
	fopen(GBA.FileLoc,"r+b");
	fseek(GBA.ROM, myoffset-0x8000000,SEEK_SET);//Tileset
	ii=GetLastError();
	for( i=0;i<0x50;i++){
		fread(&RoomTilesets[i].gTiles,4,1,GBA.ROM);
		
		fread(&RoomTilesets[i].pTiles,4,1,GBA.ROM);
		fread(&RoomTilesets[i].gBackground,4,1,GBA.ROM);
		fread(&RoomTilesets[i].TSAMap,4,1,GBA.ROM);
		fread(&RoomTilesets[i].EffectSet,4,1,GBA.ROM);  
   	}
	
    fseek(GBA.ROM,0x75FD28,SEEK_SET);
	//for( i = 0; i < 0xB;i++) fread(&ZMScrolls[i],4,1,GBA.ROM);
	fseek(GBA.ROM, 0x75FAC4,SEEK_SET);
	fread(RoomOffsets,sizeof(long),7,GBA.ROM);
//	fseek(GBA.ROM,0x75FAA8,SEEK_SET);
	//fread(DoorOffsets,sizeof(long),7,GBA.ROM);
	
	return 0;
}

int GetMFArrays(){
	int i=0;
	/*
	Get FileNo, HeaderPointers + 1, RoomOffsets
	Get FileNo, DoorPointers + 1, DoorOffsets
	Get FileNo, SpecialEffectTablePnt + 1, SpecialEffects
	Get FileNo, MiniMapBanks + 1, ZMMiniMapOffsets()
	Get FileNo, ZMScrollListBase + 1, ZMScrolls()
	   MFTileSetHeaders = &H3BF888
    MFHeaderPointers = &H79B8BC
    MFLookUpTable = &H3C8F90
    ReDim DoorOffsets(0 To 9) As Long
    DoorPointers = &H79B894
    MFSpecialEffectTablePnt = &H3C8D58
    MFScrollListBase = &H79BB08
    Enableit*//*
	//fseek(GBA.ROM,0x2E4A50,SEEK_SET);
	//fread(&MFSprSize,4,0xC0,GBA.ROM);
	fseek(GBA.ROM, 0x3BF888,SEEK_SET);//Tileset
	for(i=0;i<0x70;i++){
		fread(&MFRoomTilesets[i].gTiles,sizeof(long),1,GBA.ROM);
		fread(&MFRoomTilesets[i].pTiles,sizeof(long),1,GBA.ROM);
		fread(&MFRoomTilesets[i].gBackground,sizeof(long),1,GBA.ROM);
		fread(&MFRoomTilesets[i].TSAMap,sizeof(long),1,GBA.ROM);
		fread(&MFRoomTilesets[i].EffectSet,sizeof(long),1,GBA.ROM);  
   	}
	
	//fseek(GBA.ROM,0x79BB08,SEEK_SET);
	//for(i = 0; i < 0xB;i++) fread(&MFScrolls[i],4,1,GBA.ROM);
	fseek(GBA.ROM, 0x79B8BC,SEEK_SET);
	fread(MFRoomOffsets,4,10,GBA.ROM);
	//fseek(GBA.ROM,0x79B894,SEEK_SET);
	//fread(MFDoorOffsets,4,10,GBA.ROM);
	//fseek(GBA.ROM,0x3e419c,SEEK_SET);
	//fread(&VRAM.RAM[0x900],1,0x36E0,GBA.ROM);
	//fseek(GBA.ROM,0x58b466,SEEK_SET);
	//fread(&GBASprPal[0x40],1,0x14,GBA.ROM);
	//fseek(GBA.ROM,0x3E40F2,SEEK_SET);
	//fread(&GBASprPal[0x56],1,0x3E,GBA.ROM);
    
	return 0;
}
/*int LoadRoom(){
int Room = */
/*
int LoadHeader(long Offset){
	long blah=0;
	int BGi=0;
	fseek(GBA.ROM,Offset,SEEK_SET);
	fread(&RoomHeader.bTileset,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.bBg0,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.bBg1,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.bBg2,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.lBg3,sizeof(u32),1,GBA.ROM);
	fread(&RoomHeader.lForeground,sizeof(u32),1,GBA.ROM); 
	fread(&RoomHeader.lLevelData,sizeof(u32),1,GBA.ROM);
	fread(&RoomHeader.lBackLayer,sizeof(u32),1,GBA.ROM);
	fread(&RoomHeader.lClipData,sizeof(u32),1,GBA.ROM); 
	fread(&RoomHeader.lBackgroundTSA,sizeof(u32),1,GBA.ROM);
	fread(&RoomHeader.bUnknown1,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.TransForeground,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.iSep1,sizeof(u16),1,GBA.ROM);
	fread(&RoomHeader.lSpritePointer,sizeof(u32),1,GBA.ROM); 
	fread(&RoomHeader.bSpriteIndex1,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.bEventSwitch,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.iSep2,sizeof(u16),1,GBA.ROM);
	fread(&RoomHeader.lSpritePointer2,sizeof(u32),1,GBA.ROM); 
	fread(&RoomHeader.bSpriteIndex2,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.bEventSwitch2,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.iSeperator,sizeof(u16),1,GBA.ROM);
	fread(&RoomHeader.lSpritePointer3,sizeof(u32),1,GBA.ROM); 
	fread(&RoomHeader.bSpriteIndex3,sizeof(u8),1,GBA.ROM);
	
	fread(&RoomHeader.bMiniMapRoomX,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.bMiniMapRoomY,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.bEffect,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.bSceneryYPos,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.bNothing,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.bMusic,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.blank,sizeof(u8),1,GBA.ROM);
	
	return 0;
}


int GetLayerData(u8 compression, u8 Layer, u32 offset){
/*nRoomBuff* ELayer[5]={	&ThisForeground,&ThisLevelData,&ThisBackLayer,&ThisBackground,&ThisClipData};//Edit Layer
fseek(GBA.ROM,offset,SEEK_SET);	
	if( compression == 0x10){
		fread(&ELayer[Layer]->X,sizeof(u8),1,GBA.ROM);		//ThisLevelData.X++;		// DISCH -- added inc
		fread(&ELayer[Layer]->Y,sizeof(u8),1,GBA.ROM);		//ThisLevelData.Y++;		// DISCH -- added inc
		// sprintf(buffer,"X is %x Y is %x",ThisLevelData.X,ThisLevelData.Y);
		// sMessage(buffer);
		fread(GBA.CompBuffer,sizeof(u8),64691,GBA.ROM);
		
		//GBA.ZMUncompRle(GBA.CompBuffer, (u8*)ThisLevelData.TileBuf2D ,ThisLevelData.X*ThisLevelData.Y, 1);
		ELayer[Layer]->BImage.Create(ELayer[Layer]->X*16,ELayer[Layer]->Y*16);
		ELayer[Layer]->BImage.SetPalette(PcPalMem);
		memset(ELayer[Layer]->TileBuf2D,0,0x20000);
		ELayer[Layer]->oSize=GBA.ZMUncompRle(ELayer[Layer]->X*ELayer[Layer]->Y, GBA.CompBuffer, (u8*)&ELayer[Layer]->TileBuf2D);
	}else if( compression==0x40){
        fseek(GBA.ROM,offset,SEEK_SET);	
		ELayer[Layer]->size=fgetc(GBA.ROM);
		
			if((ELayer[Layer]->size == 1)||(ELayer[Layer]->size >= 3)) {
				ELayer[Layer]->X = 64;
				ELayer[Layer]->Y = 32;
			}else if(ThisBackground.size == 2){
				ELayer[Layer]->X = 32;
				ELayer[Layer]->Y = 64;
			}else{
				
				ELayer[Layer]->X = 32; 
				ELayer[Layer]->Y = 32;
			}


		fseek(GBA.ROM,offset+4,SEEK_SET);
		fread(&GBA.CompBuffer,sizeof(u8),32691,GBA.ROM);
		memset(ELayer[Layer]->TileBuf2D,0,0x20000);
		//ThisBackLayer.oSize=GBA.LZ77UnComp(GBA.CompBuffer, decompbuf);
        ELayer[Layer]->oSize=GBA.LZ77UnComp(GBA.CompBuffer, (u8*)ELayer[Layer]->TileBuf2D);
		ELayer[Layer]->BImage.Create(1024,1024);
		ELayer[Layer]->BImage.SetPalette(PcPalMem);
		//memcpy(ELayer[Layer]->TileBuf2D,decompbuf,ELayer[Layer]->oSize);
	}*//*
return 0;
}

int SaveLevel(FILE* fp, u32 HeaderOffset ){
	/*

//	GBA.REDIT = fopen(FilePath,"r+b");
	if(fp){
     //   while(1){ 
		GBA.ZMCompress(fp);
      //   if(RoomHeader.lBackLayer!=RoomHeader.lLevelData) break;
	//	}
	    SaveSprites();
		fseek(fp, HeaderOffset, SEEK_SET);
		
		
	
		fwrite(&RoomHeader.bTileset,sizeof(u8),1,fp);
		fwrite(&RoomHeader.bBg0,sizeof(u8),1,fp);
		fwrite(&RoomHeader.bBg1,sizeof(u8),1,fp);
		fwrite(&RoomHeader.bBg2,sizeof(u8),1,fp);
		fwrite(&RoomHeader.lBg3,sizeof(u32),1,fp);
		fwrite(&RoomHeader.lForeground,sizeof(u32),1,fp); 
		fwrite(&RoomHeader.lLevelData,sizeof(u32),1,fp);
		fwrite(&RoomHeader.lBackLayer,sizeof(u32),1,fp);
		fwrite(&RoomHeader.lClipData,sizeof(u32),1,fp); 
		fwrite(&RoomHeader.lBackgroundTSA,sizeof(u32),1,fp);
		fwrite(&RoomHeader.bUnknown1,sizeof(u8),1,fp);
		fwrite(&RoomHeader.TransForeground,sizeof(u8),1,fp);
		fwrite(&RoomHeader.iSep1,sizeof(u16),1,fp);
		fwrite(&RoomHeader.lSpritePointer,sizeof(u32),1,fp); 
		fwrite(&RoomHeader.bSpriteIndex1,sizeof(u8),1,fp);
		fwrite(&RoomHeader.bEventSwitch,sizeof(u8),1,fp);
		fwrite(&RoomHeader.iSep2,sizeof(u16),1,fp);
		fwrite(&RoomHeader.lSpritePointer2,sizeof(u32),1,fp); 
		fwrite(&RoomHeader.bSpriteIndex2,sizeof(u8),1,fp);
		fwrite(&RoomHeader.bEventSwitch2,sizeof(u8),1,fp);
		fwrite(&RoomHeader.iSeperator,sizeof(u16),1,fp);
		fwrite(&RoomHeader.lSpritePointer3,sizeof(u32),1,fp); 
		fwrite(&RoomHeader.bSpriteIndex3,sizeof(u8),1,fp);
		fwrite(&RoomHeader.bMiniMapRoomX,sizeof(u8),1,fp);
		fwrite(&RoomHeader.bMiniMapRoomY,sizeof(u8),1,fp);
		fwrite(&RoomHeader.bEffect,sizeof(u8),1,fp);
		fwrite(&RoomHeader.bSceneryYPos,sizeof(u8),1,fp);
		fwrite(&RoomHeader.bNothing,sizeof(u8),1,fp);
		fwrite(&RoomHeader.bMusic,sizeof(u8),1,fp);
		fwrite(&RoomHeader.blank,sizeof(u8),1,fp);
		
		SaveScroll();
     	SaveDoors();
	    LoadHeader(HeaderOffset);
	    
		
	}
	//fp = NULL;
	return 0;
}

int LoadZMROM(){
	
	GetArrays();
	for( int i = 0;i < 4;i++) 	 
	{
		SetUpCombos(i);
	}
	PopulateSpriteArray();
	return 0;
}


int LoadMFROM(){
	GetMFArrays();
	for( int i = 0;i < 4;i++) 	 
	{
		SetUpCombos(i);
				}
	PopulateSpriteArray();		
	return 0;
}

int LoadData(){

    if(RoomHeader.bBg0) GetLayerData(RoomHeader.bBg0, 0, RoomHeader.lForeground-0x8000000);
	if(RoomHeader.bBg1) GetLayerData(RoomHeader.bBg1, 1, RoomHeader.lLevelData-0x8000000);
	if(RoomHeader.bBg2) GetLayerData(RoomHeader.bBg2, 2, RoomHeader.lBackLayer-0x8000000);
	
	if(RoomHeader.lBg3==0x40){
		
		 GetLayerData(RoomHeader.lBg3, 3, RoomHeader.lBackgroundTSA-0x8000000);
	}else if((RoomHeader.lBg3&0xF0)==0xF0){
        GetCBG();
		}
	 GetLayerData(0x10, 4, RoomHeader.lClipData-0x8000000);*//*
	return 0;
}

/*
int DrawBackGround(Image* img){
	//We gotta do something about repeating
	
	
	int mX=ThisBackground.X;
	int mY=ThisBackground.Y;
	int difference = (mY>mX? mX:mY);
	int HorScenRep = 0;
	int VerScenRep = 0;
	nHScroll[SceneHScroll]=nVScroll[SceneVScroll]=nVScroll[STVScroll]=0;
	nMaxHScroll[SceneHScroll]=0;
	nMaxVScroll[SceneVScroll]=mY/8;
	nMaxVScroll[STVScroll]=2;//for now
	img->Create(ThisLevelData.X*16,ThisLevelData.Y*16);
	img->SetPalette(PcPalMem);
	int i , x, y, scrX, scrY;
	i=x=y=scrX=scrY=0;//
	int rex = (mX == 64 ? 2 :  1);
	int rey = (mY == 64 ? 2 :  1);
	
	
	if(mX && mY){
				for(scrY = 0; scrY < rey; scrY++)
				{	
					for(scrX = 0; scrX < rex; scrX++)
					{
						for(y = 0; y < 32; ++y)
						{
							for(x = 0; x < 32; ++x)
							{
                                for(int WRep = 0; WRep<= ThisLevelData.X/mX + 1 ;WRep++){
									for(int HRep = 0;HRep<=ThisLevelData.Y/mY + 1;HRep++){
										img->Draw(BGImage,x*8 + scrX*256 + (WRep*(mX*8)) ,y*8+ scrY*256+ (HRep*(mY*8)) ,  ThisBackground.TileBuf2D[i]);
									}
								}
								i++;
							}
						}
					}
				}
	}

				return 0; 
}


*/


/*
int DrawRoom(){
/*
	u8 cSize=0;
	u8 fd=0;
	u32 fWidth=0,fHeight=0;
	//int Width=img[1].Width,Height=img[1].Height;
	int k =0;

	u16 Width=ThisLevelData.X<<4, Height=ThisLevelData.Y<<4;
	if(!((crf == 0)||(crf==1))) return 0;
	if(DrawAll==1){


    ThisForeground.Dirty =1 ;
	ThisLevelData.Dirty =1;
	ThisBackLayer.Dirty = 1;
	DrawAll=0;
	}

	img[iRoom].Create(Width,Width);
	
	ThisBackBuffer.Create(Width, Height);
	SetBkMode(ThisBackBuffer.DC(), TRANSPARENT);
	SetTextColor(ThisBackBuffer.DC(),RGB(255,255,255));
	img[iRoom].SetPalette(PcPalMem);
	
 	
	TileImage.Load(fGbuf,1024);
    
	
	  if(RoomHeader.lBg3& 0xF0 )	DrawBackGround(&ThisBackground.BImage); 
	
     ThisBackground.BImage.Blit(ThisBackBuffer.DC(),0,0,Width ,Width,0,0);
 
	if(CheckBoxes[ViewB].value()==BST_CHECKED){
		if(ThisBackLayer.Dirty){
		    ThisBackLayer.BImage.Create(ThisBackLayer.X*16,ThisBackLayer.Y*16);
            ThisBackLayer.BImage.SetPalette(PcPalMem);
			DrawLayer(&ThisBackLayer, RoomHeader.bBg2);//Backlayer
		    ThisBackLayer.Dirty=0;
	    } 
		if(RoomHeader.lBg3&0xF0){
			ThisBackLayer.BImage.TransBlit(ThisBackBuffer.DC(),0,0,Width, Height,0,0);
		}else{
            ThisBackLayer.BImage.Blit(ThisBackBuffer.DC(),0,0,Width, Height,0,0);
		}
	
	}
	
	if(CheckBoxes[ViewL].value()){
		if(ThisLevelData.Dirty){
		    ThisLevelData.BImage.Create(ThisLevelData.X*16,ThisLevelData.Y*16);
            ThisLevelData.BImage.SetPalette(PcPalMem);	
			DrawLayer(&ThisLevelData, RoomHeader.bBg1);//Level
			ThisLevelData.Dirty=0;
			
		}
	ThisLevelData.BImage.TransBlit(ThisBackBuffer.DC(),0,0,Width, Height,0,0);
	}
	
				
				
	

	if(CheckBoxes[ViewF].value()){
		if(ThisForeground.Dirty){
		  ThisForeground.BImage.Create(ThisForeground.X*16,ThisForeground.Y*16);
          ThisForeground.BImage.SetPalette(PcPalMem);
		  DrawLayer(&ThisForeground, RoomHeader.bBg0);//Foreground
		  ThisForeground.Dirty=0;
	   	}

        if(chkDoTrans.value() == 1)
		 ThisForeground.BImage.AlphaBlit(ThisBackBuffer.DC(),0,0,Width, Height,0,0,255);
	    else
		 ThisForeground.BImage.TransBlit(ThisBackBuffer.DC(),0,0,Width, Height,0,0);
	    
	}
	
	  
	
	
	
	
	img[iRoom].TransBlit(ThisBackBuffer.DC(),0,0,Width, Height,0,0);
	ShowSprites((u8)Combos[cSpriteSet].GetListIndex());
	DisplayDoors();
	if(ViewClip.value()   ==1)DrawClipIdent();
	if(ScrollCheck.value()==1)DrawScrolls  (cboScroll.GetListIndex());
	
	InvalidateRect(hwndMap,0,1);
     
	//SendMessage(hwndMap,WM_SIZE,1,0);
*//*
	return 0;
}


int DrawLayer(nRoomBuff* Map, u8 ctype){/* /img is the image to blit to 
int thisX=0,thisY=0,mX=0,mY=0;
u16 TILE = 0;
int i =0;
int X=Map->X;
int Y=Map->Y;
int ThisY=0;
int ThisX=0;
u16* TileBuf2D=Map->TileBuf2D;
Image* pic=&Map->BImage;


pic->SetPalette(PcPalMem);
	if(ctype == 0x40){//LZ 
		//pic->Create(ThisLevelData.X*16,ThisLevelData.Y*16);
		//pic->SetPalette(PcPalMem);
			//this type uses the background set for tiles.
			
			for(int ScenRep = 0;ScenRep<ThisLevelData.X;ScenRep++){
				for(thisX=0;thisX<Map->X;thisX++){
					for(thisY=0;thisY<Map->Y;thisY++){
						pic->Draw(BGImage, (ScenRep*32)+(thisX)* 8,(RoomHeader.bSceneryYPos-1)*16 + (thisY) * 8, TileBuf2D[(thisX)+(thisY*X )]);
					}
				}
			}
		
	}else 
	if(ctype == 0x10){
	//	for(thisY=Map->DrawRect.top;thisY<(Map->DrawRect.bottom);thisY++){

	//		for(thisX = Map->DrawRect.left; thisX < (Map->DrawRect.right);thisX++){// from here if something is enabled then draw it 
		for(thisY=0;thisY<(Map->Y);thisY++){

			for(thisX=0;thisX<(Map->X);thisX++){// from here if something is enabled then draw it 
						TILE =(TileBuf2D[thisX+(thisY * X)]);
						BitBlt(pic->DC(),(thisX)<<4, (thisY)<<4,16,16,img[iTileset].DC(),(TILE%16)<<4,(TILE>>4)<<4,SRCCOPY);
					
	   
				}
			}
		
		//Rect to draw

	}
   
*//*

	return 0;
}

*/
